
var saveLink = function(){
	var get_url = document.location.href; 
	
	$.each(go_panel.model.linkDataArray, function(index, value){
		var node_xml;
		
		if(!value['visit']){
			node_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://www.e-servicesuite.com:8088/ws-doc/"><soapenv:Header><ws:authentication><username>webservice</username><password>web</password></ws:authentication></soapenv:Header><soapenv:Body><ws:DeleteLinkByID><LinkNbr>2</LinkNbr><DownstreamCMDB_ID>' + value.data['DownstreamCMDB_ID'] + '</DownstreamCMDB_ID><UpstreamCMDB_ID>' + value.data['UpstreamCMDB_ID'] + '</UpstreamCMDB_ID></ws:DeleteLinkByID></soapenv:Body></soapenv:Envelope>';
		}
	
		
		if(value['stroke'] != $('#Topology').val() && value['stroke'] != $('#Service').val()){
			node_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://www.e-servicesuite.com:8088/ws-doc/"><soapenv:Header><ws:authentication><username>webservice</username><password>web</password></ws:authentication></soapenv:Header><soapenv:Body><ws:CreateTopologyLink><AllowedLinkNbr>2</AllowedLinkNbr><DownstreamCMDB_ID>'+value.data['DownstreamCMDB_ID']+'</DownstreamCMDB_ID><UpstreamCMDB_ID>'+value.data['UpstreamCMDB_ID']+'</UpstreamCMDB_ID></ws:CreateTopologyLink></soapenv:Body></soapenv:Envelope>';
		}
				
        $.ajax({
            type : "POST",
                contentType : "text/xml",
                url : "/ws/SearchCMDB",
                data : node_xml,// 这里不该用JSON格式
                dataType : 'xml',// 这里设成XML或者不设。设成JSON格式会让返回值变成NULL
                success : function(xml) {
                
                	
                	if($(xml).find('CreateTopologyLinkResponse').text() != ''){
                		debug_console('Create :' + $(xml).find('Result').text())
                	} else {
                		debug_console('Del :' + $(xml).find('Result').text())
                	}
                	
          
                }
             });
		
	});  
	
	
	// 用户验证
	var query_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://www.e-servicesuite.com:8088/ws-doc/">'+
	'<soapenv:Header><ws:authentication><username>webservice</username><password>web</password></ws:authentication></soapenv:Header><soapenv:Body><ws:UserCanModify><UID>' + 
	get_url.substring(get_url.indexOf("?")+1).split("&").toString().substr(3)  +'</UID></ws:UserCanModify></soapenv:Body></soapenv:Envelope>';	
	
	
                 $.ajax({
                type : "POST",
                    contentType : "text/xml",
                    url : "/ws/SearchCMDB",
                    data : query_xml,// 这里不该用JSON格式
                    dataType : 'xml',// 这里设成XML或者不设。设成JSON格式会让返回值变成NULL
                    success : function(xml) {
                        if($(xml).find('Result') == false){
                        	alert('User is wrong')
                        } else {
                      		
                        }
                    }
                 });
}


var delNode = function(){
	 var selSet = go_panel.selection;
	        var it = selSet.iterator;
	        while (it.next()) {
	                  var obj = it.value;
	                if(obj instanceof go.Node) {
	                        go_panel.commandHandler.deleteSelection();
	                        continue;
	                }
	                if(obj instanceof go.Link) {
	                        obj.visible = false;
	                        obj.Tn['visit'] = false;
	        continue;
	                }
	        }
	
}



var saveAsImage = function(){
var oCanvas = document.getElementsByTagName('canvas')[0];
    //debug_console(oCanvas);
    Canvas2Image.saveAsPNG(oCanvas)
}


var saveFile = function(){	
	//debug_console(go_panel.model.toJson())
    var data = "data:x-application/text,"+encodeURIComponent(go_panel.model.toJson());
    window.open(data); 
}


var openFile = function(file){
	if(file){
		var reader = new FileReader();
		  reader.onload = function(e){
             go_panel.layout.isInitial = false;
            go_panel.model = go.Model.fromJson(e.target.result);  
            go_panel.layout.isOngoing = true;

		  }  	  
		  reader.readAsText(file,'UTF-8')
		  // debug_console(reader.readAsText(file,'UTF-8'))
	}
}





var layerChange = function(){
    
        
        if($('#Topology').attr('checked') == 'checked'){ 
            line_color = $('#Topology').val()
            for(var i = 0 ; i < go_panel.model.linkDataArray.length ; i++){
                //go_panel.select(go_panel.findLinkForData(go_panel.model.linkDataArray[i]));
                go_panel.findLinkForData(go_panel.model.linkDataArray[i]).visible = true;
                

                
                if(go_panel.model.linkDataArray[i].stroke == $('#Service').val() || go_panel.model.linkDataArray[i].stroke == service_none ){    
                    go_panel.findLinkForData(go_panel.model.linkDataArray[i]).visible = false;
                }
            }    
            };
                    
        if($('#Service').attr('checked') == 'checked'){ 
            line_color = $('#Service').val()
            for(var i = 0 ; i < go_panel.model.linkDataArray.length ; i++){
                //go_panel.select(go_panel.findLinkForData(go_panel.model.linkDataArray[i]));
                go_panel.findLinkForData(go_panel.model.linkDataArray[i]).visible = true;
                
                if(go_panel.model.linkDataArray[i].stroke == $('#Topology').val() || go_panel.model.linkDataArray[i].stroke == topology_none){
                    
                    go_panel.findLinkForData(go_panel.model.linkDataArray[i]).visible = false;
                }
            }
        };
        
        
        if( $('#Service').attr('checked') == 'checked' && $('#Topology').attr('checked') == 'checked'){
            for(var i = 0 ; i < go_panel.model.linkDataArray.length ; i++){
                //go_panel.select(go_panel.findLinkForData(go_panel.model.linkDataArray[i]));
                go_panel.findLinkForData(go_panel.model.linkDataArray[i]).visible = true;
            }    
        }
        
        if( $('#Service').attr('checked') != 'checked' && $('#Topology').attr('checked') != 'checked'){
            for(var i = 0 ; i < go_panel.model.linkDataArray.length ; i++){
                //go_panel.select(go_panel.findLinkForData(go_panel.model.linkDataArray[i]));
                go_panel.findLinkForData(go_panel.model.linkDataArray[i]).visible = false;
            }
            
        }    
}


var lineInit = function(id){
    $.each(go_panel.model.nodeDataArray,function(i,cbj){
        var query_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://www.e-servicesuite.com:8088/ws-doc/"><soapenv:Header><ws:authentication><username>webservice</username><password>web</password></ws:authentication></soapenv:Header><soapenv:Body><ws:GetLinksAllowed><DownstreamCMDB_ID>' + id +'</DownstreamCMDB_ID><UpstreamCMDB_ID>' + cbj.data.cmdb_id + '</UpstreamCMDB_ID></ws:GetLinksAllowed></soapenv:Body></soapenv:Envelope>';

        if(id!=cbj.data.cmdb_id){
                 $.ajax({
                type : "POST",
                    contentType : "text/xml",
                    url : "/ws/SearchCMDB",
                    data : query_xml,// 这里不该用JSON格式
                    dataType : 'xml',// 这里设成XML或者不设。设成JSON格式会让返回值变成NULL
                    success : function(xml) {
                        var obj = $(xml).find('AllowedLinks');
                        if( obj.length > 0){
                            for(var c = 0; c<obj.length; c++){
                                if(!$('#' + $(obj[c]).find('LinkLayer').text()).is(':checked')){
                                    if(!confirm('The link must be created in a '+$(obj[c]).find('LinkLayer').text()+' layer, but the layer is unchecked.If you really want to create it, the layer will be checked.')){
                                                continue;
                                    }
                                }                              
                                $('#' + $(obj[c]).find('LinkLayer').text()).attr('checked',true);
                                
                                var newlink = { from: cbj.data.cmdb_id, to:id, stroke: $('#' + $(obj[c]).find('LinkLayer').text()).val(),bottomlabel: $(obj[c]).find('BottomLabel').text(), toplabel: $(obj[c]).find('TopLabel').text(),visit:true, data:{DownstreamCMDB_ID:id , UpstreamCMDB_ID:cbj.data.cmdb_id}}
                                //go_panel.allowSelect = false;
                                go_panel.model.addLinkData(newlink);
                               
                      
                                layerChange();
                                //go_panel.allowSelect = true;
                                
        
                            }
                        }
                        
                        
                        
                    }
                 });
            
        }
        
        
        
        
        query_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://www.e-servicesuite.com:8088/ws-doc/"><soapenv:Header><ws:authentication><username>webservice</username><password>web</password></ws:authentication></soapenv:Header><soapenv:Body><ws:GetLinksAllowed><DownstreamCMDB_ID>' + cbj.data.cmdb_id +'</DownstreamCMDB_ID><UpstreamCMDB_ID>' + id + '</UpstreamCMDB_ID></ws:GetLinksAllowed></soapenv:Body></soapenv:Envelope>';        

        if(id!=cbj.data.cmdb_id){
                 $.ajax({
                type : "POST",
                    contentType : "text/xml",
                    url : "/ws/SearchCMDB",
                    data : query_xml,// 这里不该用JSON格式
                    dataType : 'xml',// 这里设成XML或者不设。设成JSON格式会让返回值变成NULL
                    success : function(xml) {
                        // debug_console(xml)
                        var obj = $(xml).find('AllowedLinks');
                        if( obj.length > 0){
                            for(var c = 0; c<obj.length; c++){
                                // $(obj[i])
                                // debug_console($(obj[c]).find('LinkLayer').text())
                                if(!$('#' + $(obj[c]).find('LinkLayer').text()).is(':checked')){
                                    // debug_console('没有选择')
                                    // (return confirm("Do you open no view
									// Layer?")) == true ? '': break;
                                    if(!confirm('The link must be created in a '+$(obj[c]).find('LinkLayer').text()+' layer, but the layer is unchecked.If you really want to create it, the layer will be checked.')){
                                                continue;
                                    }
                                }
                                
                
                                
                                $('#' + $(obj[c]).find('LinkLayer').text()).attr('checked',true);
                                
                                
                                var newlink = { from:id , to: cbj.data.cmdb_id, stroke: $('#' + $(obj[c]).find('LinkLayer').text()).val(),bottomlabel: $(obj[c]).find('BottomLabel').text(), toplabel: $(obj[c]).find('TopLabel').text(),visit:true, data:{DownstreamCMDB_ID:id , UpstreamCMDB_ID:cbj.data.cmdb_id}}
                                //go_panel.allowSelect = false;
                                go_panel.model.addLinkData(newlink);
                              
                                layerChange();
                                //go_panel.allowSelect = true;
                                
                            }
                        }
                        
                        
                        
                    }
                 });
            
        }
        
      
    })
    
    
    
    
    // alert($('#Topology').attr('checked'))
 
    
    
}

var tableDaag = function(){
    var index;
    
    $("#showimg").easydrag(true);
    $("#showimg").ondrop(function(event){ 
        
        if($('#canvas').offset().left <= event.clientX && $('#canvas').offset().top <= event.clientY && event.clientY < (document.getElementById('canvas').offsetHeight + $('#canvas').offset().top) && event.clientX < (document.getElementById('canvas').offsetWidth+$('#canvas').offset().left)){
            
        var obj =  $('#ci_list_grid tr').eq(index);
        var is_same = true;
        for(var i = 0 ; i < go_panel.model.nodeDataArray.length ; i++){
            go_panel.model.nodeDataArray[i].data['cmdb_id'] == obj.find('td').eq(1).html() ? is_same = false : '';
        };
        
        if(is_same == true){
            var new_node = {
                    key: obj.find('td').eq(1).html(),
                    label: obj.find('td').eq(0).html(),
                    is_in_service: obj.find('input[name="status"]').val() == 'In_Use' ? true : false,  
                    location:(event.clientX - $('#canvas').offset().left)+' '+(event.clientY - $('#canvas').offset().top),
                    desiredSize_1: "32 32",
                    desiredSize_2: "32 32",
                    img_url: obj.find('input[name="url"]').val(),
                    data:{cmdb_id:obj.find('td').eq(1).html(),
                    visible	:true
                    }
        };    
            go_panel.model.addNodeData(new_node);
            lineInit(obj.find('td').eq(1).html());
            
        } else {
            
            alert('Node is exist');
        }
                    
            
        }
        
        $("#showimg").hide();
    });
      
     $('#ci_list_grid tr').bind('mousedown',function(event){
         $("#showimg").attr('src', $(this).find('input[name="url"]').val());
             $("#showimg").trigger("mousedown");
             $("#showimg").css('top',event.clientY- 10);
            $("#showimg").css('left',event.clientX - 10);
            $("#showimg").show();
            index = $(this).index();
         });
         

     
}


var showLinkProperties = function(e){
     if(e.isSelected) {
         var query_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://www.e-servicesuite.com:8088/ws-doc/"><soapenv:Header><ws:authentication><username>webservice</username><password>web</password></ws:authentication></soapenv:Header><soapenv:Body><ws:GetLinksAllowed><DownstreamCMDB_ID>' + e.toNode.data.key +'</DownstreamCMDB_ID><UpstreamCMDB_ID>' + e.fromNode.data.key + '</UpstreamCMDB_ID></ws:GetLinksAllowed></soapenv:Body></soapenv:Envelope>';
         $.ajax({
            type : "POST",
            contentType : "text/xml",
            url : "/ws/SearchCMDB",
            data : query_xml,// 这里不该用JSON格式
            dataType : 'xml',// 这里设成XML或者不设。设成JSON格式会让返回值变成NULL
            success : function(xml) {
                setLinkProperties(xml)            
            }
         });
         
         query_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://www.e-servicesuite.com:8088/ws-doc/"><soapenv:Header><ws:authentication><username>webservice</username><password>web</password></ws:authentication></soapenv:Header><soapenv:Body><ws:GetUpstreamLinks><CMDB_ID>'+e.toNode.data.data.cmdb_id+'</CMDB_ID></ws:GetUpstreamLinks></soapenv:Body></soapenv:Envelope>';
         
         
         $.ajax({
             type : "POST",
             contentType : "text/xml",
             url : "/ws/SearchCMDB",
             data : query_xml,// 这里不该用JSON格式
             dataType : 'xml',// 这里设成XML或者不设。设成JSON格式会让返回值变成NULL
             success : function(xml) {
            	 //var from_id = $(xml).find('')
            	 
            	 //debug_console(xml);
            	 
            	 setLinkAttribute(xml, e.fromNode.data.data.cmdb_id);
            	 
             }
          });
        
}
     
     
     
     if(e.isSelected) {
e.elt(2).visible = true;
e.elt(3).visible = true;
}else {
e.elt(2).visible = false;
e.elt(3).visible = false;
}
}

var setLinkProperties = function(xml){
    var html = '<tr class="k-alt"><td>AllowedLinkNbr</td><td>'+$(xml).find('AllowedLinkNbr').text()+'</td></tr>'
        + '<tr><td>LinkLayer</td><td>'+$(xml).find('LinkLayer').text()+'</td></tr>'
        + '<tr class="k-alt"><td>TopLabel</td><td>' + $(xml).find('TopLabel').text() +'</td></tr>'
        + '<tr><td>BottomLabel</td><td>'+$(xml).find('BottomLabel').text()+'</td></tr>';    
    $('#properties_grid tbody').html(html);       
}


var setLinkAttribute = function(xml, from_node_id){
	var from_id = $(xml).find('CMDB_ID');
	
	debug_console(xml)
	for(var i=0; i<from_id.length; i++){
		//debug_console('find:'+$(from_id[i]).text() +'----' + 'now:'+from_node_id);
		if( $(from_id[i]).text() == from_node_id){
			debug_console('true');
			var html = '';
//			for(var c = 0 ; c < $(xml).children().length; c++){
//				debug_console($(xml).children().text())
//			}
			
//			$.each($(xml).children(),function(c, value){
//				debug_console(c)
//			})
			
			var c = 1;
			while(c){
				if($(xml).find('Link_Attribute_'+c).length>0){
					
					html += '<tr><td>Link_Attribute_'+c+'</td><td>'+$(xml).find('Link_Attribute_'+c).text()+'</td></tr>';
					c++;
				} else{
					break;
				}
			}
			
			$('#attributes_grid tbody').html(html);
			
			
		} else {
			debug_console('false');
		}
	}
	
}

var validateLink = function(e){    
    var query_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://www.e-servicesuite.com:8088/ws-doc/"><soapenv:Header><ws:authentication><username>webservice</username><password>web</password></ws:authentication></soapenv:Header><soapenv:Body><ws:GetLinksAllowed><DownstreamCMDB_ID>' + e.subject.toNode.data.key +'</DownstreamCMDB_ID><UpstreamCMDB_ID>' + e.subject.fromNode.data.key + '</UpstreamCMDB_ID></ws:GetLinksAllowed></soapenv:Body></soapenv:Envelope>';    
    $.ajax({
            type : "POST",
            contentType : "text/xml",
            url : "/ws/SearchCMDB",
            data : query_xml,// 这里不该用JSON格式
            dataType : 'xml',// 这里设成XML或者不设。设成JSON格式会让返回值变成NULL
            success : function(xml) {
                if($(xml).find('AllowedLinks').length == 0){
              
               if($('#Topology').attr('checked') == 'checked'){ 
               	    e.subject.elt(0).stroke = topology_none;
               	    e.subject.data.stroke =  topology_none;
               	    e.subject.data.data = {layer:'Topology',DownstreamCMDB_ID: e.subject.toNode.data.key, UpstreamCMDB_ID:e.subject.fromNode.data.key};
               };
               
               
               
              if($('#Service').attr('checked') == 'checked'){                     
                     e.subject.elt(0).stroke = service_none;
                     e.subject.data.stroke =  service_none;
                     e.subject.data.data = {layer:'Service',DownstreamCMDB_ID: e.subject.toNode.data.key, UpstreamCMDB_ID:e.subject.fromNode.data.key};
               };
               
               
              if( $('#Service').attr('checked') != 'checked' && $('#Topology').attr('checked') != 'checked'){
            	  
            	  alert('Player choose one Layer');
            	  go_panel.commandHandler.deleteSelection()
                }
              
              if( $('#Service').attr('checked') == 'checked' && $('#Topology').attr('checked') == 'checked'){
            	  alert('Player keep one Layer');
            	  go_panel.commandHandler.deleteSelection()
            	  
              }
                    
                    
                    
                    
                    
                } else {
                    setLinkProperties(xml)
                }            
            }
    });
}


var setSerachPanel = function(){
    for(var i = 0; i<$('select').length ; i++){
        if( i == 0){
            $('select').eq(i).val('')
        } else {
            $('select').eq(i).html('');
            $('select').eq(i).val('')
            $('select').eq(i).attr('disabled',true);
        }
    }
}


var getSearch = function() {
    var cxml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://www.e-servicesuite.com:8088/ws-doc/"><soapenv:Header><ws:authentication><username>webservice</username><password>web</password></ws:authentication></soapenv:Header><soapenv:Body><ws:searchCMDB><CIClassType>'
            + ($('#ci_class_type_select').val() == null ? '':$('#ci_class_type_select').val())
            + '</CIClassType><Company>'
            + ($('#cutonmer_select').val() == null ? '':$('#cutonmer_select').val())
            + '</Company><nameStr>'
            + ($('#ci_name').val() == null ?'' : $('#ci_name').val())
            + '</nameStr><CIClass>'
            + ($('#ci_class_select').val() == null ? '' : $('#ci_class_select').val())
            + '</CIClass><CISuperClass>'
            + ($('#ci_super_class_select').val() == null ? '' :$('#ci_super_class_select').val())
            + '</CISuperClass></ws:searchCMDB></soapenv:Body></soapenv:Envelope>';

    $.ajax({
            type : "POST",
            contentType : "text/xml",
            url : "/ws/SearchCMDB",
            data : cxml,// 这里不该用JSON格式
            dataType : 'xml',// 这里设成XML或者不设。设成JSON格式会让返回值变成NULL
            success : function(xml) {
                var html = '';
                var data_obj = $(xml).find('Result');
                ci_list_panel.title('CI List ('+data_obj.length+' records found)');
                for (var i = 0; i < data_obj.length; i++) {
                    html += '<tr class="'+ (i % 2 == 0 ? 'k-alt':'') +'"><input type="hidden" value="'+ $(data_obj[i]).find('Xtreme_Image_Path').text()+'" name="url" /><input type="hidden" name="status" value="'+$(data_obj[i]).find('CoreStatus').eq(0).text()+'" /><td>' + $($(data_obj[i]).find('Name')[0]).text()
                            + '</td><td>'
                            + $(data_obj[i]).find('CMDB_ID').text()
                            + '</td><td>'
                            + $(data_obj[i]).find('Description').text()
                            + '</td><td>'
                            + $(data_obj[i]).find('SerialNbr').text()
                            + '</td><td>'
                            + $(data_obj[i]).find('Usage').text()
                            + '</td><td>'
                            + $('#ci_class_type_select').val()
                            + '</td><td>'
                            + $('#ci_super_class_select').val()
                            + '</td><td>' + $('#ci_class_select').val()
                            + '</td><td>'
                            + $(data_obj[i]).find('Status').find('Status').text()
                            + '</td></tr>';
                    };
                $("#ci_list_grid tbody").html(html);
                if(data_obj.length == 0){
                    setBlankCIList();
                }
                
                tableDaag();        
                }
                
            });
        
}

var getCI_Class = function() {

    if ($('#ci_super_class_select').val() != '') {
        var query_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://www.e-servicesuite.com:8088/ws-doc/"><soapenv:Header><ws:authentication><username>webservice</username><password>web</password></ws:authentication></soapenv:Header><soapenv:Body><ws:SearchCIClass><CIClassTypeStr>'
                + $('#ci_class_type_select').val()
                + '</CIClassTypeStr><!--Optional:--><CompanyStr>'
                + $('#cutonmer_select').val()
                + '</CompanyStr><!--Optional:--><CIClassStr></CIClassStr><CISuperClassStr>'
                + $('#ci_super_class_select').val()
                + '</CISuperClassStr></ws:SearchCIClass></soapenv:Body></soapenv:Envelope>';
        $.ajax({
                    type : "POST",
                    contentType : "text/xml",
                    url : "/ws/SearchCMDB",
                    data : query_xml,// 这里不该用JSON格式
                    dataType : 'xml',// 这里设成XML或者不设。设成JSON格式会让返回值变成NULL
                    success : function(xml) {
                        var data_obj = $(xml).find('CIClass').find('CIClass');
                        var html = '<option value="">&nbsp;</option>';
                        for (var i = 0; i < data_obj.length; i++) {
                            html += '<option value="' + $(data_obj[i]).text()
                                    + '">' + $(data_obj[i]).text()
                                    + '</option>';
                        }

                        $('#ci_class_select').attr('disabled', false);
                        $('#ci_class_select').html(html)
                    }
                });
    } else {
        $('#ci_class_select').attr('value','');
        $('#ci_class_select').attr('disabled', true);
    }

}

var getCI_Super_Class = function() {

    if ($('#ci_class_type_select').val() != '') {
        var query_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://www.e-servicesuite.com:8088/ws-doc/"> <soapenv:Header> <ws:authentication><username>webservice</username><password>web</password></ws:authentication></soapenv:Header><soapenv:Body><ws:SearchCISuperClass><CIClassTypeStr>'
                + $('#ci_class_type_select').val()
                + '</CIClassTypeStr><!--Optional:--> <CompanyStr>'
                + $('#cutonmer_select').val()
                + '</CompanyStr><!--Optional:--><CISuperClassStr></CISuperClassStr> </ws:SearchCISuperClass></soapenv:Body></soapenv:Envelope>';
        $.ajax({
            type : "POST",
            contentType : "text/xml",
            url : "/ws/SearchCMDB",
            data : query_xml,// 这里不该用JSON格式
            dataType : 'xml',// 这里设成XML或者不设。设成JSON格式会让返回值变成NULL
            success : function(xml) {
                var data_obj = $(xml).find('CISuperClass').find('CISuperClass');
                var html = '<option value="">&nbsp;</option>';
                for (var i = 0; i < data_obj.length; i++) {
                    html += '<option value="' + $(data_obj[i]).text() + '">'
                            + $(data_obj[i]).text() + '</option>';
                }
                $('#ci_super_class_select').html(html)
            }
        });
        $('#ci_super_class_select').attr('disabled', false);
        $('#ci_super_class_select').unbind('change');
        $('#ci_super_class_select').bind('change', function() {
                    getCI_Class();
                });
    } else {
        
        $('#ci_super_class_select').attr('value','');
        $('#ci_super_class_select').attr('disabled', true);
        $('#ci_class_select').attr('value','');
        $('#ci_class_select').attr('disabled', true);
    }

}

var getSearchCIClassType = function() {
    if ($('#cutonmer_select').val() != '') {
        var query_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://www.e-servicesuite.com:8088/ws-doc/"><soapenv:Header><ws:authentication><username>webservice</username><password>web</password></ws:authentication></soapenv:Header><soapenv:Body><ws:SearchCIClassType><CIClassTypeStr></CIClassTypeStr><CompanyStr>'
                + $('#cutonmer_select').val()
                + '</CompanyStr></ws:SearchCIClassType></soapenv:Body></soapenv:Envelope>';
        $.ajax({
                    type : "POST",
                    contentType : "text/xml",
                    url : "/ws/SearchCMDB",
                    data : query_xml,// 这里不该用JSON格式
                    dataType : 'xml',// 这里设成XML或者不设。设成JSON格式会让返回值变成NULL
                    success : function(xml) {
                        var data_obj = $(xml).find('CIClassType')
                                .find('CIClassType');
                        var html = '<option value="">&nbsp;</option>';
                        for (var i = 0; i < data_obj.length; i++) {
                            html += '<option value="' + $(data_obj[i]).text()
                                    + '">' + $(data_obj[i]).text()
                                    + '</option>';
                        }
                        $('#ci_class_type_select').html(html)
                    }
                });
        $('#ci_class_type_select').attr('disabled', false);
        $('#ci_class_type_select').unbind('change');
        $('#ci_class_type_select').bind('change', function() {
        getCI_Super_Class();
                });

    } else {
        $('#ci_class_type_select').attr('value','');
        $('#ci_class_type_select').attr('disabled', true);

    }

}

var getCutonmer = function() {
    var query_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://www.e-servicesuite.com:8088/ws-doc/"><soapenv:Header><ws:authentication><username>webservice</username><password>web</password></ws:authentication></soapenv:Header><soapenv:Body><ws:searchCustomer><!--Optional:--><CompanyStr></CompanyStr></ws:searchCustomer></soapenv:Body></soapenv:Envelope>';
    $.ajax({
                type : "POST",
                contentType : "text/xml",
                url : "/ws/SearchCMDB",
                data : query_xml,// 这里不该用JSON格式
                dataType : 'xml',// 这里设成XML或者不设。设成JSON格式会让返回值变成NULL
                success : function(xml) {
                    var data_obj = $(xml).find('Customer');
                    var html = '<option value="">&nbsp;</option>';
                    for (var i = 0; i < data_obj.length; i++) {
                        html += '<option value="' + $(data_obj[i]).text()
                                + '">' + $(data_obj[i]).text() + '</option>';
                    }
                    $('#cutonmer_select').html(html)
                }
            });

    $('#cutonmer_select').attr('disabled', false);
    $('#cutonmer_select').unbind('change');
    $('#cutonmer_select').bind('change', function() {
                getSearchCIClassType();
            });

}


var node_click = function(e){
    if(e.isSelected) {
         var query_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://www.e-servicesuite.com:8088/ws-doc/"><soapenv:Header><ws:authentication><username>webservice</username><password>web</password></ws:authentication></soapenv:Header><soapenv:Body><ws:SearchCMDBID><CMDB_ID>'+e.data.data.cmdb_id+'</CMDB_ID></ws:SearchCMDBID></soapenv:Body></soapenv:Envelope>';         
         $.ajax({
            type : "POST",
            contentType : "text/xml",
            url : "/ws/SearchCMDB",
            data : query_xml,
            dataType : 'xml',
            success : function(xml) {
                var html='';
                var attr = $(xml).find('Attribute');
                var properties = $(xml).find('CoreCI');    
                html += '<tr class="k-alt"><td>CMDB ID</td><td>'+ $(properties).find('CMDB_ID').text() + '</td></tr>' + 
                        '<tr><td>Status</td><td>' + $(properties).find('Status').find('Status').text() + '</td></tr>' +
                        '<tr class="k-alt"><td>Name</td><td>'+ $(properties).find('Name').text() +'</td></tr>' +
                        '<tr><td>Description</td><td>+' + $(properties).find('Description').text() + '+</td></tr>' +
                        // '<tr class="k-alt"><td>SOX
						// applies</td><td>'+$(properties).find('Description').text()+'</td></tr>'
						// +
                        '<tr><td>ISEC applies</td><td>' + $(properties).find('ISEC_Applies').text() + '</td></tr>' + 
                        '<tr class="k-alt"><td>Criticality</td><td>'+ $(properties).find('Description').text()+'</td></tr>' +
                        '<tr><td>Serial #</td><td>'+ $(properties).find('SerialNbr').text() +'</td></tr>' +
                        '<tr class="k-alt"><td>Usage</td><td>' + $(properties).find('Usage').text()+ '</td></tr>' +
                        '<tr><td>Class Type</td><td>'+$(properties).find('CIClass').find('CIClass').text()+'</td></tr>' +
                        '<tr class="k-alt"><td>Super-Class</td><td>'+$(properties).find('CISuperClass').find('CISuperClass').text()+'</td></tr>' + 
                        '<tr><td>CI Class</td><td>'+$(properties).find('CIClass').find('CIClass').text()+'</td></tr>' + 
                        '<tr class="k-alt"><td>Building</td><td>'+$(properties).find('Building').find('Name').text()+'</td></tr>' +
                        '<tr><td>Floor</td><td>'+$(properties).find('Floor').find('Floor').text()+'</td></tr>' +
                        '<tr class="k-alt"><td>Room</td><td>'+$(properties).find('Room').find('Room').text()+'</td></tr>' +
                        '<tr><td>Make</td><td>'+$(properties).find('Make').text()+'</td></tr>' + 
                        '<tr class="k-alt"><td>Model</td><td>'+$(properties).find('Model').text()+'</td></tr>' +
                        '<tr><td>Type</td><td>'+$(properties).find('CIType').text()+'</td></tr>' +
                        '<tr class="k-alt"><td>Address Code</td><td>'+$(properties).find('AddressCode').text()+'</td></tr>' +
                        '<tr><td>Form Factor</td><td>'+$(properties).find('FormFactor').text()+'</td></tr>' ;
                
                if( html == '' ){
                    for(var i = 1 ; i<= 20; i++){
                        html += '<tr class="'+ (i % 2 == 0 ? '':'k-alt')+'"><td >&nbsp;</td><td ></td></tr>';
                    };
                };
                $('#properties_grid tbody').html(html);                
                
                
                html = '';        
                var i = 0;        
                
                
                
                $('#attributes_grid tbody').html(html);

            }
            });
    }
}


